﻿using System;
using System.Collections.Generic;
using System.Text;
using Framework.Library.Core.Utils.Database;

namespace Framework.Library.Data.DAL
{
    [Serializable]
    public class DALEntity
    {
        [Serializable]
        public class ColumnInfo
        {
            public string Name { get; set; }
            public string Alias { get; set; }
            public string TableName { get; set; }

            public override string ToString()
            {
                StringBuilder builder = new StringBuilder();
                if (TableName != null)
                    builder.AppendFormat("[{0}].", TableName);
                builder.AppendFormat("[{0}]", Name);
                if (Alias != null)
                    builder.AppendFormat(" AS [{0}]", Alias);
                return builder.ToString();
            }
        }

        public string Key = string.Empty;
        public string Table = string.Empty;
        public string Description = string.Empty;
        public string OrderBy = string.Empty;
        public SortOrder OrderSort = SortOrder.Ascending;

        public Dictionary<string, object> lst = new Dictionary<string, object>();
        public Dictionary<object, string> lstColumn = new Dictionary<object, string>();
        public List<ColumnInfo> _columns = new List<ColumnInfo>();

        public T GetValue<T>(string name)
        {
            if (lst[name] != null)
                return (T)lst[name];
            else
                return default(T);
        }

        public void SetValue(string name, object value)
        {
            if(lst.ContainsKey(name))
                lst[name] = value;
            else
                lst.Add(name, value);
        }

        public void AddColumn(string colName)
        {
            AddColumn(colName, null, null);
        }

        public void AddColumn(string colName, string tableName)
        {
            AddColumn(colName, tableName, null);
        }

        public void AddColumn(string colName, string tableName, string alias)
        {
            _columns.Add(new ColumnInfo() { Name = colName, TableName = tableName, Alias = alias });
        }
    }
}